package cn.edu.neusoft.food729.fragment;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.retroiftdemo_729.R;

import java.util.List;

import cn.edu.neusoft.food729.Bean.UserCollectionBean;
import cn.edu.neusoft.food729.adapter.FoodCollectionAdapter;
import cn.edu.neusoft.food729.listener.UserCollectionListener;
import cn.edu.neusoft.food729.model.UserCollectionModel;

import static android.content.Context.MODE_PRIVATE;

public class Fragment22 extends BaseFragment {

    private int page = 1;
    private int lastVisibleItemPosition;

    private List<UserCollectionBean> list;
    private LinearLayoutManager layoutManager;
    private FoodCollectionAdapter adapter;
    private RecyclerView recyclerView;
    private SharedPreferences sp;//简单存储
    private String sessionID = "";

    private UserCollectionListener listener = new UserCollectionListener() {
        @Override
        public void onResponse(List<UserCollectionBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter = new FoodCollectionAdapter(context);
            adapter.setList(list);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败" + msg, Toast.LENGTH_SHORT).show();
        }
    };

    public Fragment22() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.fragment22, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        System.out.println("--f22--" + getSessionId());//父类
        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        sessionID = sp.getString("session", null);
        initRecycleView(view);
        UserCollectionModel model = new UserCollectionModel();
        model.getUserCollectionResult(Integer.valueOf(sessionID), 1, listener);
    }

    private void initRecycleView(View view) {
        recyclerView = view.findViewById(R.id.collect_food);
        // 设置布局
        layoutManager = new LinearLayoutManager(context);
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
//设置适配器

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (list != null) {
                    if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                        page += 1;
                        UserCollectionModel model = new UserCollectionModel();
                        model.getUserCollectionResult(Integer.valueOf(sessionID), 1, listener);
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();//滚动结束后将赋
            }
        });
    }
}
